package com.oristartech.netty.client.starter;

import com.oristartech.netty.common.dispatcher.ChannelSender;
import com.oristartech.netty.common.message.NoticeMessage;
import com.oristartech.netty.common.message.RequestMessage;
import com.oristartech.netty.common.message.ResponseMessage;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 ** 客户端发送
 *
 * @author: chenfenghai
 * @version: 1.0
 * @date: 2020-03-17 16:44
 * @blame Team
 */
@Slf4j
@Component
public class NettyClientSender {


    /**
     ** 请求发送对象
     */
    @Resource
    ChannelSender channelSender;

    /**
     ** 当前客户端通道
     */
    private Channel channel;

    /**
     ** 设置当前客户端通道
     *
     * @param channel 当前客户端通道
     */
    public void setChannel(Channel channel) {
        this.channel = channel;
    }

    /**
     ** 清空当前客户端通道
     */
    public void clear() {
        this.channel = null;
    }

    /**
     ** 发送通知
     *
     * @param message 通知消息
     */
    public void sendNotice(NoticeMessage message){
        channelSender.sendNotice(channel, message);
    }

    /**
     ** 发送请求并接收响应结果
     *
     * @param message 请求消息
     * @return 响应结果
     */
    public ResponseMessage sendRequest(RequestMessage message){
        return channelSender.sendRequest(channel, message);
    }
}
